#include "DemoCharacter.h"

#include "Camera/CameraComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "../Component/DamageComponent.h"
#include "NiagaraComponent.h"
#include "Components/AudioComponent.h"
#include "Demo/UI/GameHUD.h"

ADemoCharacter::ADemoCharacter()
{
	PrimaryActorTick . bCanEverTick = true;

	bUseControllerRotationPitch = false;
	bUseControllerRotationYaw = false;
	bUseControllerRotationRoll = false;

	GetCharacterMovement() -> bOrientRotationToMovement = true;
	GetCharacterMovement() -> RotationRate = FRotator(0, 600, 0);

	SpringArmComponent = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComponent"));
	SpringArmComponent -> TargetArmLength = 400;
	SpringArmComponent -> bUsePawnControlRotation = true;
	SpringArmComponent -> bDoCollisionTest = false;
	SpringArmComponent -> SetupAttachment(GetRootComponent());

	CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
	CameraComponent -> bUsePawnControlRotation = false;
	CameraComponent -> SetupAttachment(SpringArmComponent);

	DamageComponent = CreateDefaultSubobject<UDamageComponent>(TEXT("DamageComponent"));

	AudioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("AudioComponent"));
	AudioComponent -> bAutoActivate = false;
	AudioComponent -> SetupAttachment(GetRootComponent());

	NiagaraComponent = CreateDefaultSubobject<UNiagaraComponent>(TEXT("NiagaraComponent"));
	NiagaraComponent -> bAutoActivate = false;
	NiagaraComponent -> SetupAttachment(GetRootComponent());
}

void ADemoCharacter::BeginPlay()
{
	Super::BeginPlay();
	DamageComponent -> AddOnDeath(this, &ThisClass::OnDeath);
	GLog -> Log("Hello Worldb");
	if (GameHUDSubclassOf)
	{
		APlayerController* PlayerController = Cast<APlayerController>(GetController());
		if (PlayerController)
		{
			GameHUD = CreateWidget<UGameHUD>(PlayerController, GameHUDSubclassOf);
			if (GameHUD)
			{
				GameHUD -> AddToViewport();
			}
		}
	}
}

void ADemoCharacter::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

void ADemoCharacter::PlayAttackSound()
{
	AudioComponent -> SetSound(AttackSound);
	AudioComponent -> Activate();
}

void ADemoCharacter::PlayAttackEffect()
{
	NiagaraComponent -> SetAsset(AttackEffect);
	NiagaraComponent -> Activate(true);
}

void ADemoCharacter::ShowHUD()
{
	if (GameHUD)
	{
		GameHUD -> Show();
	}
}

void ADemoCharacter::PlayDamageHUD()
{
	if (GameHUD)
	{
		GameHUD -> PlayDamageAnimation();
	}
}

void ADemoCharacter::OnDeath()
{
	if (GEngine)
	{
		GEngine -> AddOnScreenDebugMessage(-1, 5, FColor::Green, TEXT("已死亡"));
	}
	GetMesh() -> SetSimulatePhysics(true);
}
